#!/bin/bash
set -e
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash OpenLB-1.5-0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
if [[ "$buildpath" = "$installpath" ]]; then echo "构建路径和安装路径相同，请重新输入"; exit 1; fi


yum install -y cmake gcc gcc-c++ wget git

cd $buildpath
wget -t 0 -c -P $buildpath https://www.openlb.net/wp-content/uploads/2022/04/olb-1.5r0.tgz
tar -xvf $buildpath/olb-1.5r0.tgz -C $buildpath && rm -rf $buildpath/olb-1.5r0.tgz
cd $buildpath/olb-1.5r0

sed -i "s/-march=native//g" config.mk
sed -i "s/g++/mpic++/g" config.mk
sed -i "s/:= gcc/:= mpicc/g" config.mk

cd $buildpath/olb-1.5r0/src/utilities
sed -i "s/for_each_n/for_each/g" benchmarkUtil.h


export CC=mpicc
export CXX=mpic++ 


cd $buildpath/olb-1.5r0
make

cd $buildpath/olb-1.5r0/examples/thermal/rayleighBenard2d
make
./rayleighBenard2d


ls -d */

zip -r tmp.zip tmp
cp tmp.zip $installpath


